package cn.jltx.mvpdemopro;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import cn.jltx.mvpdemopro.config.ErroCode;
import cn.jltx.mvpdemopro.model.User;
import cn.jltx.mvpdemopro.presenter.ILoginPresenter;
import cn.jltx.mvpdemopro.presenter.LoginPresenterImp;
import cn.jltx.mvpdemopro.view.ILoginView;

public class MainActivity extends BaseActivity<ILoginView,ILoginPresenter> implements ILoginView,View.OnClickListener{
    private EditText etLoginUsername;
    private EditText etLoginPassword;
    private Button doLogin;
    private Button doClear;
    private ProgressBar progressLogin;
    private TextView userTip;


    private void assignViews() {
        etLoginUsername = (EditText) findViewById(R.id.et_login_username);
        etLoginPassword = (EditText) findViewById(R.id.et_login_password);
        doLogin = (Button) findViewById(R.id.do_login);
        doClear = (Button) findViewById(R.id.do_clear);
        userTip = (TextView)findViewById(R.id.user_tip);
        progressLogin = (ProgressBar) findViewById(R.id.progress_login);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();

        doLogin.setOnClickListener(this);
        doClear.setOnClickListener(this);
        progressLogin.setVisibility(View.INVISIBLE);
    }

    @Override
    public ILoginPresenter initPresenter() {
        return new LoginPresenterImp();
    }

    @Override
    public void initUser(String text) {
        userTip.setText(text);
    }

    @Override
    public void onSuccess(User user) {
        progressLogin.setVisibility(View.INVISIBLE);
        doLogin.setEnabled(true);
        doClear.setEnabled(true);
        Intent intent = new Intent(this,UserActivity.class);
        String userName = presenter.getUsername() != null ? presenter.getUsername() : "";
        String userPw = presenter.getPassword() != null ? presenter.getPassword() : "";
        intent.putExtra("user",userName);
        intent.putExtra("password",userPw);
        startActivity(intent);
        finish();
    }

    @Override
    public void onFailure(int erroCode) {
        progressLogin.setVisibility(View.INVISIBLE);
        doLogin.setEnabled(true);
        doClear.setEnabled(true);
        if(erroCode == ErroCode.LOGIN_PASSWORD_ERROR){
            Toast.makeText(this,"登录失败，密码错误!",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"登录失败，用户错误!",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClearText() {
        etLoginUsername.setText("");
        etLoginPassword.setText("");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.do_login:
                String userName = etLoginUsername.getText().toString();
                String userPw = etLoginPassword.getText().toString();
                doLogin.setEnabled(false);
                doClear.setEnabled(false);
                progressLogin.setVisibility(View.VISIBLE);
                presenter.login(userName,userPw);
                break;
            case R.id.do_clear:
                presenter.clear();
                break;
            default:
                break;
        }
    }
}
